package JDBC;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import Model.Stage;

public class Stage_Ajouter {
	public int ajouter(Stage stage)
	{
		int id=1;
		
		try 
		{	

			connexionBDD a = new connexionBDD();
			Connection conn=a.connect();

			Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

			Statement state1 = conn.createStatement();
			ResultSet result = state1.executeQuery("Select MAX(id_stage) from stage");
						
			//On récupère les MetaData pour les prof
			ResultSetMetaData resultMeta = result.getMetaData();
			
			try
			{
				while(result.next())
				{
					id=Integer.parseInt(result.getObject(1).toString())+1;
				}
			}
			catch(Exception e)
			{		
			}
			
			result.close();
			
			state.executeUpdate("INSERT INTO stage VALUES ("+id+",'"+stage.getDateStage()+"',"+stage.getDuree()+",'"+stage.getHeureDebut()+"','"+stage.getHeureFin()+"',"+stage.getId_Lieu()+","+stage.getId_Prof()+","+stage.getId_Mat()+","+stage.getId_Niv()+");");

			state.close();
		 
		} 

		catch (Exception e) {
			e.printStackTrace();
		}	
		return id;

	}
	
	public void associerElevesStage(String idEleve, int idStage)
	{
		try
		{
			connexionBDD c=new connexionBDD();
			Connection conn=c.connect();
			Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
			int idEleve2=Integer.parseInt(idEleve);							
				state.executeUpdate("INSERT INTO eleves_stage VALUES ("+idEleve2+","+idStage+");");
				
			state.close();
		}
		
		catch (Exception e) {
			e.printStackTrace();
		}	
	}

}
